package chapter3.xmlBean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import chapter0.HelloWorld;
public class Chapter3XmlTest {
	public void	validateCommon(ApplicationContext context){
		HelloBean hello = (HelloBean) context.getBean("helloBean");
		hello.print();
	}
	public void	validateScope(ApplicationContext context){
		HelloWorld obj1 = (HelloWorld) context.getBean("scopeBean1");
		obj1.setMessage("I am obj1");
		obj1.getMessage();
		HelloWorld obj2 = (HelloWorld) context.getBean("scopeBean1");
		obj2.getMessage();

		HelloWorld obj3 = (HelloWorld) context.getBean("scopeBean2");
		obj3.setMessage("I am obj1");
		obj3.getMessage();
		HelloWorld obj4 = (HelloWorld) context.getBean("scopeBean2");
		obj4.getMessage();
	}

	public void	validateLife(ApplicationContext context){
//		HelloWorld obj1 = (HelloWorld) context.getBean("scopeBean3");
//		obj1.setMessage("I am obj1");
//		obj1.getMessage();
//		HelloWorld obj2 = (HelloWorld) context.getBean("scopeBean3");
//		obj2.getMessage();

		HelloWorld obj3 = (HelloWorld) context.getBean("scopeBean5");
		obj3.setMessage("I am obj3");
		obj3.getMessage();
//		HelloWorld obj4 = (HelloWorld) context.getBean("scopeBean4");
//		obj4.getMessage();
	}
	public void	validateSetter(ApplicationContext context){
		//Setter注入验证
		Person person = (Person)context.getBean("person");
		person.sayHello();
	}
	public void	validateConstruct(ApplicationContext context){
		//构造函数注入
		Person person1 = (Person)context.getBean("person1");
		person1.sayHello();
	}
	public static void main(String[] args) {
		Chapter3XmlTest obj = new Chapter3XmlTest();
		//ApplicationContext context = new ClassPathXmlApplicationContext("chapter3_xml_bean.xml");
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("chapter3_xml_bean.xml");
		//obj.validateCommon(context);
		//obj.validateScope(context);
		obj.validateLife(context);
		context.registerShutdownHook();
		//context.close();
	}
}
